import type { Configuration } from 'webpack';
import { mergeWithRules } from 'webpack-merge';
import grafanaConfig from './.config/webpack/webpack.config';
import path from "path";

const config = async (env: any): Promise<Configuration> => {
    const baseConfig = await grafanaConfig(env);
    const customConfig = {
        module: {
            rules: [
                {
                    exclude: /(node_modules)/,
                    test: /\.[tj]sx?$/,
                    use: {
                        loader: 'swc-loader',
                        options: {
                            jsc: {
                                baseUrl: path.resolve(process.cwd(), 'src'),
                                target: 'es2015',
                                loose: false,
                                parser: {
                                    syntax: 'typescript',
                                    tsx: true,
                                    decorators: false,
                                    dynamicImport: true
                                },
                                transform: {
                                    legacyDecorator: true,
                                    decoratorMetadata: true,
                                    react: {
                                        runtime: "automatic", // or classic
                                        throwIfNamespace: true,
                                        useBuiltins: true,
                                        development: true
                                    },
                                },
                            },
                        },
                    },
                },
            ],
        },
        resolve: {

        },
    };
    const mergedRules = mergeWithRules({
        module: {
        },
    })(baseConfig, customConfig);
    console.log('################# mergedRules.module.rules', JSON.stringify(mergedRules.module.rules));
    return mergedRules;
};

export default config;
